package br.org.etec.dotcao.dao.jpa;

import java.util.Collections;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import br.org.etec.dotcao.dao.RaceDao;
import br.org.etec.dotcao.model.Race;

public class RaceDaoImpl extends JPAImpl<Race> implements RaceDao {

	public RaceDaoImpl() {
		super(Race.class);
	}

	@Override
	public List<Race> all(long specieCode) {
		TypedQuery<Race> query = em.createNamedQuery("selectBySpecie", Race.class);
		query.setParameter("specie", specieCode);
		
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return Collections.emptyList();
		}
	}
	
	@Override
	public Race get(String name) {
		TypedQuery<Race> query = em.createNamedQuery("selectByName", Race.class);
		query.setParameter("name", name);
		
		try {
			return query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
}
